#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
长尾关键词挖掘系统 - 演示程序

这个演示程序展示了如何使用长尾关键词挖掘系统的各个功能模块。
包含了完整的数据流程演示和功能测试。
"""

import sys
import os
import pandas as pd
import numpy as np
from datetime import datetime

# 添加src目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

from src.utils import load_config, setup_logging
from main import KeywordMiningPipeline


def create_demo_data():
    """创建演示数据"""
    print("🔧 创建演示数据...")
    
    # 创建一些示例关键词
    demo_keywords = [
        "Python编程入门", "机器学习算法", "数据分析工具", "深度学习框架",
        "人工智能应用", "数据挖掘技术", "统计学基础", "可视化图表",
        "SEO优化技巧", "关键词研究", "搜索引擎营销", "内容营销策略",
        "电商运营", "社交媒体营销", "品牌推广", "用户体验设计",
        "网站开发", "前端框架", "后端架构", "数据库设计",
        "云计算服务", "大数据处理", "区块链技术", "物联网应用",
        "移动应用开发", "游戏开发", "软件测试", "项目管理",
        "投资理财", "股票分析", "基金投资", "保险规划",
        "健康养生", "美食制作", "旅游攻略", "摄影技巧",
        "在线教育", "职业规划", "技能培训", "语言学习"
    ]
    
    # 创建DataFrame
    demo_data = []
    for i, keyword in enumerate(demo_keywords, 1):
        demo_data.append({
            'id': i,
            'keyword': keyword,
            'length': len(keyword),
            'source': 'demo',
            'collected_time': datetime.now()
        })
    
    df = pd.DataFrame(demo_data)
    
    # 确保目录存在
    os.makedirs('data/raw', exist_ok=True)
    
    # 保存演示数据
    df.to_csv('data/raw/collected_keywords.csv', index=False, encoding='utf-8-sig')
    
    print(f"✅ 演示数据创建完成，共 {len(demo_keywords)} 个关键词")
    return df


def run_demo():
    """运行演示程序"""
    print("=" * 60)
    print("🎯 长尾关键词挖掘系统 - 演示程序")
    print("=" * 60)
    print()
    
    try:
        # 创建演示数据
        demo_df = create_demo_data()
        
        print("📊 演示数据预览:")
        print(demo_df.head(10).to_string(index=False))
        print()
        
        # 初始化流水线
        print("🚀 初始化长尾关键词挖掘系统...")
        pipeline = KeywordMiningPipeline()
        
        # 演示数据清洗
        print("\n" + "=" * 40)
        print("🧹 演示数据清洗功能")
        print("=" * 40)
        
        cleaned_df = pipeline.run_data_cleaning(demo_df)
        
        print(f"清洗前关键词数量: {len(demo_df)}")
        print(f"清洗后关键词数量: {len(cleaned_df)}")
        print("\n清洗后数据预览:")
        print(cleaned_df[['keyword', 'length', 'complexity_score']].head(10).to_string(index=False))
        
        # 演示关键词分类
        print("\n" + "=" * 40)
        print("🏷️  演示关键词分类功能")
        print("=" * 40)
        
        classified_df = pipeline.run_classification(cleaned_df)
        
        print(f"分类后关键词数量: {len(classified_df)}")
        print(f"聚类数量: {classified_df['cluster_id'].nunique()}")
        print(f"分类数量: {classified_df['ml_category'].nunique()}")
        
        print("\n分类结果预览:")
        sample_df = classified_df[['keyword', 'ml_category', 'cluster_id', 'classification_confidence']].head(10)
        print(sample_df.to_string(index=False))
        
        # 显示分类统计
        print("\n📈 分类统计:")
        category_counts = classified_df['ml_category'].value_counts()
        for category, count in category_counts.items():
            percentage = count / len(classified_df) * 100
            print(f"  {category}: {count} 个 ({percentage:.1f}%)")
        
        # 演示可视化功能
        print("\n" + "=" * 40)
        print("📊 演示可视化功能")
        print("=" * 40)
        
        try:
            pipeline.run_visualization(classified_df)
            print("✅ 可视化图表已生成，请查看 data/results/visualizations/ 目录")
        except Exception as e:
            print(f"⚠️  可视化演示跳过 (可能缺少显示环境): {e}")
        
        # 生成演示报告
        print("\n" + "=" * 40)
        print("📋 演示总结报告")
        print("=" * 40)
        
        report = {
            '原始关键词数量': len(demo_df),
            '清洗后关键词数量': len(cleaned_df),
            '分类后关键词数量': len(classified_df),
            '数据清洗率': f"{(len(demo_df) - len(cleaned_df)) / len(demo_df) * 100:.1f}%",
            '聚类数量': int(classified_df['cluster_id'].nunique()),
            '分类数量': int(classified_df['ml_category'].nunique()),
            '平均分类置信度': f"{classified_df['classification_confidence'].mean():.3f}",
            '最高置信度关键词': classified_df.loc[classified_df['classification_confidence'].idxmax(), 'keyword']
        }
        
        for key, value in report.items():
            print(f"  {key}: {value}")
        
        print("\n" + "=" * 60)
        print("🎉 演示程序执行完成！")
        print("=" * 60)
        print("\n📁 生成的文件:")
        print("  - data/raw/collected_keywords.csv (原始数据)")
        print("  - data/cleaned/cleaned_keywords.csv (清洗后数据)")  
        print("  - data/results/classified_keywords.csv (分类结果)")
        print("  - data/results/visualizations/ (可视化图表)")
        print("  - data/results/*.json (分析报告)")
        
        print("\n💡 提示:")
        print("  - 运行 'python main.py --all' 执行完整流程")
        print("  - 运行 'python main.py --help' 查看所有选项")
        print("  - 修改 config/config.yaml 自定义配置")
        
    except Exception as e:
        print(f"\n❌ 演示程序执行失败: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    run_demo()
